/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/idenger">idenger</a> All rights reserved.
 */
package com.idenger.modules.project.web.equipment;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.idenger.common.utils.Constant;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.idenger.common.config.Global;
import com.idenger.common.persistence.Page;
import com.idenger.common.web.BaseController;
import com.idenger.common.utils.StringUtils;
import com.idenger.modules.project.entity.equipment.Equipment;
import com.idenger.modules.project.service.equipment.EquipmentService;

/**
 * 设备Controller
 * @author chenlin
 * @version 2018-04-11
 */
@Controller
@RequestMapping(value = "${adminPath}/project/equipment/equipment")
public class EquipmentController extends BaseController {

	@Autowired
	private EquipmentService equipmentService;
	
	@ModelAttribute
	public Equipment get(@RequestParam(required=false) String id) {
		Equipment entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = equipmentService.get(id);
		}
		if (entity == null){
			entity = new Equipment();
		}
		return entity;
	}
	
	@RequiresPermissions("project:equipment:equipment:view")
	@RequestMapping(value = {"list", ""})
	public String list(Equipment equipment, HttpServletRequest request, HttpServletResponse response, Model model) {
		String projectId = request.getParameter("projectId");
		if(StringUtils.isNotBlank(projectId)){
			equipment.setProjectId(projectId);
		}
		Page<Equipment> page = equipmentService.findPage(new Page<Equipment>(request, response), equipment); 
		model.addAttribute("page", page);
		model.addAttribute("projectId", projectId);

		return "modules/project/equipment/equipmentList";
	}

	@RequiresPermissions("project:equipment:equipment:view")
	@RequestMapping(value = "form")
	public String form(HttpServletRequest request,Equipment equipment, Model model) {

		if(StringUtils.isNotBlank(equipment.getProjectId())){
			model.addAttribute("projectId",equipment.getProjectId());
		}else{
			model.addAttribute("projectId",request.getParameter("projectId"));
		}
		model.addAttribute("equipment", equipment);
		return "modules/project/equipment/equipmentForm";
	}

	@RequiresPermissions("project:equipment:equipment:edit")
	@RequestMapping(value = "save")
	public String save(HttpServletRequest request,Equipment equipment, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, equipment)){
			return form(request,equipment, model);
		}
		String projectId = request.getParameter("projectId");
		equipment.setProjectId(projectId);
		equipment.setDeleteFlag(Constant.NORMAL_STATUS);
		equipmentService.save(equipment);
		addMessage(redirectAttributes, "保存设备成功");
		return "redirect:"+Global.getAdminPath()+"/project/equipment/equipment/?repage";
	}
	
	@RequiresPermissions("project:equipment:equipment:edit")
	@RequestMapping(value = "delete")
	public String delete(Equipment equipment, RedirectAttributes redirectAttributes) {
		equipmentService.delete(equipment);
		addMessage(redirectAttributes, "删除设备成功");
		return "redirect:"+Global.getAdminPath()+"/project/equipment/equipment/?repage";
	}

}